package host.skiree.nettydemo.protocol;

import host.skiree.nettydemo.message.LoginRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author gcl
 * @date 2022/9/16 14:19
 */
public class MessageCodecTest {
    public static void main(String[] args) throws Exception {
        EmbeddedChannel channel = new EmbeddedChannel(
                new LoggingHandler(LogLevel.DEBUG),
                /**
                 * ||   magicNum  ||version||serializerType||messageType|| sequenceId  ||     长度     ||填充 ||
                 * || 14 16 09 10 ||  01   ||      00      ||     00    || 00 00 00 00 || 00 00 00 de || ff ||
                 * || 0  1  2  3      4            5              6        7  8  9  10    11 12 13 14    15
                 */
                new LengthFieldBasedFrameDecoder(1024, 11,4,1,0),
                new MessageCodec());

        LoginRequestMessage message = new LoginRequestMessage("zhangsan", "123");

        // 测试编码
        channel.writeOutbound(message);

        // 测试解码
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null, message, buf);

        // 入栈
        channel.writeInbound(buf);
    }
}
